/*
 * Copyright (c) Shenzhen 7th Road Technical Co., Ltd
 * Technical Support dept.
 * http://www.7road.com
 */
package com.sz7thrd.ts.ccu.filter.codec;

import java.nio.charset.Charset;
import java.nio.charset.CharsetEncoder;
import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.session.AttributeKey;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolEncoderAdapter;
import org.apache.mina.filter.codec.ProtocolEncoderOutput;
import org.apache.mina.filter.codec.textline.LineDelimiter;

/**
 * @author able
 * @date 2011-12-14
 * ===============================
 * 
 * 
 */
public class MyEncoder extends ProtocolEncoderAdapter {

    private final AttributeKey ENCODER = new AttributeKey(getClass(), "encoder");
    private final Charset charset;
    private final LineDelimiter delimiter;
    private int maxLineLength = Integer.MAX_VALUE;
    
    public MyEncoder() {
        this(Charset.defaultCharset(), LineDelimiter.UNIX);
    }

    public MyEncoder(LineDelimiter delimiter) {
        this(Charset.defaultCharset(), delimiter);
    }

    public MyEncoder(String delimiter) {
        this(new LineDelimiter(delimiter));
    }

    public MyEncoder(Charset charset) {
        this(charset, LineDelimiter.UNIX);
    }

    public MyEncoder(Charset charset, String delimiter) {
        this(charset, new LineDelimiter(delimiter));
    }

    public MyEncoder(Charset charset, LineDelimiter delimiter) {
        if (charset == null) {
            throw new IllegalArgumentException("charset");
        }
        if (delimiter == null) {
            throw new IllegalArgumentException("delimiter");
        }
        if (LineDelimiter.AUTO.equals(delimiter)) {
            throw new IllegalArgumentException(
                    "AUTO delimiter is not allowed for encoder.");
        }

        this.charset = charset;
        this.delimiter = delimiter;
    }
    
    public int getMaxLineLength() {
        return maxLineLength;
    }
    
    public void setMaxLineLength(int maxLineLength) {
        if (maxLineLength <= 0) {
            throw new IllegalArgumentException("maxLineLength: "
                    + maxLineLength);
        }
        this.maxLineLength = maxLineLength;
    }
    
    public void dispose() throws Exception {
        // Do nothing
    }

    public void encode(IoSession session, Object message, ProtocolEncoderOutput out) throws Exception {
        CharsetEncoder encoder = (CharsetEncoder) session.getAttribute(ENCODER);

        if (encoder == null) {
            encoder = charset.newEncoder();
            session.setAttribute(ENCODER, encoder);
        }
        
        System.out.println("=> encode");

        String value = (message == null ? "" : message.toString());
        IoBuffer buf = IoBuffer.allocate(value.length()).setAutoExpand(true);
        buf.putString(value, encoder);

        if (buf.position() > maxLineLength) {
            throw new IllegalArgumentException("Line length: " + buf.position());
        }

        buf.putString(delimiter.getValue(), encoder);
        buf.flip();
        out.write(buf);
    }
}
